﻿/**
 * 
 *	LoaderImgStream.getInstance().loadPhoto(photo_mc,"img.jpg");
 *	LoaderImgStream.getInstance().loadPhoto(photo_mc,"img.jpg",loading_mc);
 */
package com._public._net 
{
	
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.net.*;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	/**
	 * 边加载边显示图片
	 * @lbynet 
	 */
	public class LoaderImgStream 
	{
		
		
		private var byteArray:ByteArray;
		private var urlStream:URLStream;
		private var isInit:Boolean;
		
		//public static var Instance:LoaderImgStream = new LoaderImgStream();
		private var path:String;
		private var photo_mc:*;
		private var _loader:Loader;
		private var loading_mc:*;
		private var complete_fun:Function;
		
		private var newWidth:Number;
		private var newHeight:Number;
		public function LoaderImgStream() 
		{
			//if(Instance!=null){
			//	throw new Error("单件")
			//}
		}
		
		/*
		 * 单件类的实例 
		*/
		//public static function getInstance():LoaderImgStream
		//{
			//return Instance;
		//}
		//
		//
		//public function setStaticObjectNull():void
		//{
		    //Instance = null;
		//}
		
		/**
		* 加载图片;
		* @param _photo_mc:DisplayObjectContainer ,path:String
		* @return
		*/
		public function loadPhoto(photo:*, path:String = "", complete_fun:Function = null, loading:MovieClip = null, _newWidth:Number = 120, _newHeight:Number = 160):void	
		{
			
			this.photo_mc = photo;
			this.path = path;
			this.complete_fun = complete_fun;
			this.loading_mc = loading;
			
			this.newWidth = _newWidth;
			this.newHeight = _newHeight;
			
			photo_mc.graphics.clear();
			//if (photo_mc.numChildren > 0)
			//{
				//this.removeAllItem(photo_mc);
			//}
			
			var urlRequest:URLRequest = new URLRequest(path);
			
			this._loader = new Loader();
			this._loader.contentLoaderInfo.addEventListener(Event.INIT, completeHandler);
			
			this.byteArray = new ByteArray();
			this.urlStream = new URLStream();
			//urlStream.endian = Endian.LITTLE_ENDIAN;
			this.urlStream.addEventListener(Event.OPEN, openHandler);
			this.urlStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			this.urlStream.addEventListener(Event.COMPLETE, completeStreamHandler);
			this.urlStream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			this.urlStream.load(urlRequest);
			
			
			this.isLoad = false;
			this.photo_mc.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
		
		private function completeStreamHandler(event:Event):void
		{
			//trace('completeStreamHandler');
			
			if (this.loading_mc)
			{
				loading_mc.visible = false;
				loading_mc.dotsAnimation_mc.gotoAndStop(1);
			}
		}
		
		
		/**
		* 停止加载;
		* @param 
		* @return
		*/
		public function stopLoad():void
		{
			
			if (this.urlStream.hasEventListener(Event.COMPLETE))
			{
				this.urlStream.removeEventListener(Event.OPEN, openHandler);
				this.urlStream.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				this.urlStream.removeEventListener(Event.COMPLETE, completeStreamHandler);
				this.urlStream.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				
				this.urlStream.removeEventListener(Event.COMPLETE, completeHandler);
			}
			if (this.photo_mc.hasEventListener(Event.ENTER_FRAME))
			{
				this.photo_mc.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			}
			if (this._loader.contentLoaderInfo.hasEventListener(Event.INIT))
			{
				this._loader.contentLoaderInfo.removeEventListener(Event.INIT, completeHandler);
			}
		}
		
		private function openHandler(event:Event):void
		{
			
			if (this.loading_mc) 
			{
				loading_mc.visible = true;
				loading_mc.dotsAnimation_mc.gotoAndPlay(2);
			}
		}
		
		private var bytesTotal:Number;
		private function progressHandler(event:ProgressEvent):void 
		{
			//trace(event.bytesTotal);
			if (isNaN(bytesTotal))
			{
				this.bytesTotal = event.bytesTotal;
			}
			
			if (this.loading_mc)
			{
				var Num:uint = uint((event.bytesLoaded/event.bytesTotal)*100);
				loading_mc.percent_txt.text = Num;
			}
			
		}
		private function completeHandler(event:Event):void 
		{
			
			if (isInit == false)
			{
				var img:DisplayObject = this._loader.content as DisplayObject;
				//trace("宽和高", img.width, img.height);
				if (img.width > 0)
				{
					isInit = true;
					addBitMap(this._loader.content);
				}
			}
			else
			{
				addBitMap(this._loader.content);
			}
			
			if (isNaN(bytesTotal) == false)
			{
				if (this.bytesTotal == byteArray.length && this.isLoad)
				{
					//trace('--------------------------------------');
					this.urlStream.removeEventListener(Event.COMPLETE, completeHandler);
					this.photo_mc.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
					this._loader.contentLoaderInfo.removeEventListener(Event.INIT, completeHandler);
					
					if (this.complete_fun != null) 
					{
						complete_fun(_loader.content);
					}
				}
			}
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			
			if (this.complete_fun != null) 
			{
				complete_fun(null);
			}
			
		    if (this.loading_mc)
			{
			   loading_mc.visible = true;
			   loading_mc.percent_txt.text = "0";
			   loading_mc.dotsAnimation_mc.gotoAndPlay(2);
			}
        }
		
		private var isLoad:Boolean;
		private function enterFrameHandler(event:Event):void
		{
			
			//trace('urlStream.bytesAvailable = ' + urlStream.bytesAvailable);
			
			if (urlStream.bytesAvailable == 0)
			{
				this.isLoad = true;
				
				//trace('this.currZero = ' + this.currZero);
				//if (this.currZero > 10)
				//{
					//
					//this.urlStream.removeEventListener(Event.COMPLETE, completeHandler);
					//this.photo_mc.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
					//this._loader.contentLoaderInfo.removeEventListener(Event.INIT, completeHandler);
					//
					//if (this.complete_fun != null) 
					//{
						//complete_fun(_loader.content);
					//}
					//
				//}
				return;
			}
			this.isLoad = false;
			
			
			this.urlStream.readBytes(byteArray, byteArray.length, urlStream.bytesAvailable);
			//imageLoader.unload();
			this._loader.loadBytes(byteArray);
		}
		
		
		private var _scale:Number;
		// 返回
		private function addBitMap(target:DisplayObject):void 
		{
			if (this.newWidth == 0)
			{
				_scale = 1;
			}
			else
			{
				_scale = this.newWidth / target.width;
			}
			
			var newBitmapData:BitmapData = new BitmapData(target.width, target.height, true, 0);
			newBitmapData.draw(target);
			
			drawBg(newBitmapData);
		}
		
		private function drawBg(bitmap:BitmapData):void
		{
			
			//创建矩阵，缩放n倍
			var matrix:Matrix = new Matrix();
			matrix.scale(_scale, _scale);
			
			this.photo_mc.graphics.clear();
			this.photo_mc.graphics.beginBitmapFill(bitmap, matrix);//位图缩放
			this.photo_mc.graphics.drawRect(0, 0, bitmap.width * _scale, bitmap.height * _scale);
			this.photo_mc.graphics.endFill();
		}
		
		
		/*
		 * 
		 * @移除所有项
		*/
		public function removeAllItem(content_mc:DisplayObjectContainer):void
		{
			var count:int = content_mc.numChildren;
			for (var i:int = 0; i < count; i++) 
			{
				content_mc.removeChildAt(0);
			}
			
		}
		
	}
	
}